package com.kould.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.kould.enity.Article;
import com.kould.repository.ArticleMapper;
import com.kould.repository.KacheArticleMapper;
import com.kould.service.ArticleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cache.annotation.CacheEvict;
import org.springframework.cache.annotation.CachePut;
import org.springframework.cache.annotation.Cacheable;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ArticleServiceImpl extends ServiceImpl<ArticleMapper, Article> implements ArticleService{

    @Autowired
    private KacheArticleMapper kacheArticleMapper;

    @Override
    public IPage<Article> findArticlesForKache(IPage<Article> page) {
        return this.kacheArticleMapper.selectPage(page, null);
    }

    @Override
    public Article findArticleByIdForKache(Long id) {
        return this.kacheArticleMapper.selectById(id);
    }

    @Override
    public IPage<Article> findArticlesTitleLikeForKache(IPage<Article> page, String title) {
        return this.kacheArticleMapper.selectPage(page, new QueryWrapper<Article>().like("title", title));
    }

    @Override
    @Cacheable(cacheNames = "article", sync = true, keyGenerator = "articleKeyGenerator")
    public IPage<Article> findArticlesForSpring(IPage<Article> page) {
        return this.page(page);
    }

    @Override
    @Cacheable(cacheNames = "id", sync = true)
    public Article findArticleByIdForSpring(Long id) {
        return this.getById(id);
    }

    @Override
    @Cacheable(cacheNames = "title", sync = true)
    public IPage<Article> findArticlesTitleLikeForSpring(IPage<Article> page, String title) {
        return this.page(page, new QueryWrapper<Article>().like("title", title));
    }

    @Override
    @CachePut
    public Integer insertArticleForKacheAndSpring(Article article) {
        return this.kacheArticleMapper.insert(article);
    }

    @Override
    @CachePut
    public Integer insertBatchArticleForKacheAndSpring(List<Article> list) {
        return this.kacheArticleMapper.insertBatchSomeColumn(list);
    }

    @Override
    @CacheEvict
    public Integer updateArticleForKacheAndSpringByTitle(Article article) {
        return this.kacheArticleMapper.update(article, lambdaQuery().eq(Article::getTitle, article.getTitle()));
    }

    @Override
    @CacheEvict
    public Integer updateArticleForKacheAndSpringById(Article article) {
        return this.kacheArticleMapper.updateById(article);
    }

    @Override
    @CacheEvict
    public Integer deleteArticleForKacheAndSpringByTitle(String title) {
        return this.kacheArticleMapper.delete(lambdaQuery().eq(Article::getTitle, title));
    }

    @Override
    @CacheEvict
    public Integer deleteArticleForKacheAndSpringById(Long id) {
        return this.kacheArticleMapper.deleteById(id);
    }
}
